var url;
function newDetail(){
    $('#dlg').dialog('open').dialog('center').dialog('setTitle','新增客户信息');
    $('#fm').form('clear');
    url = '/Lease/saveDetail.json';
}
function saveDetail(){
    $("#formOrderId").val($("#orderId").val());
    $('#fm').form('submit',{
        url: url,
        onSubmit: function(){
            return $(this).form('validate');
        },
        success: function(result){
            if (result == "fail"){
                $.messager.show({
                    title: 'Error',
                    msg: result.errorMsg
                });
            } else {
                $('#dlg').dialog('close');        // close the dialog
                $('#dg').datagrid('reload');    // reload the user data
            }
        }
    });
}
function saveStopped(){
$('#fm-bt').form('submit',{
        url: url,
        onSubmit: function(){
            return $(this).form('validate');
        },
        success: function(result){
        var result = eval('('+result+')');
            if (result.errorMsg){
                $.messager.alert("失败",result.errorMsg);
            } else {
                $('#dlg-bt').dialog('close');        // close the dialog
                $('#dg').datagrid('reload');    // reload the user data
            }
        }
    });
}
$(function(){
    $('#dg').datagrid({
        url:"/Lease/detail.json?orderId="+$("#orderId").val(),
        columns:[[
            {field:'id',title:'ID'},
            {field:'name',title:'名称'},
            {field:'number',title:'产品数量'},
            {field:'unitPrice',title:'单价'},
            {
                field:'algorithm',
                title:'计算方式',
                formatter:function(value,row,index){
                    if(value == "ONCE"){
                        return "一次";
                    }else if(value == "BYDAY"){
                        return "天数";
                    }else if(value == "BYAREA"){
                        return "面积";
                    }else if(value == "FIXEDDAY"){
                        return "固定天数";
                    }else{
                        return value;
                    }
                }
            },
            {field:'params',title:'参数(天/平米)'},
            {field:'returnNumber',title:'已归还数量'},
            { field:'start',title:'出借日期'},
            {
                field:'stop',
                title:'归还日期',
                formatter:function(value,row,index){
                    if(value){
                        return value;
                    }else{
                        return "未归还";
                    }
                }
            },
            {field:'price',title:'应交租金(截止今天)'},
            {field:'remark',title:'备注'}
        ]],
        view: detailview,
        detailFormatter:function(index,row){
            return '<div style="padding:2px"><table class="ddv"></table></div>';
        },
        onExpandRow: function(index,row){
            var ddv = $(this).datagrid('getRowDetail',index).find('table.ddv');
            ddv.datagrid({
                url:'/Lease/stopped.json?detailId='+row.id,
                fitColumns:true,
                singleSelect:true,
                height:'auto',
                columns:[[
                    {field:'number',title:'归还数量'},
                    {field:'dayNumber',title:'归还天数'},
                    {field:'stop',title:'归还时间',align:'right'},
                    {field:'price',title:'价格'}
                ]],
                onResize:function(){
                    $('#dg').datagrid('fixDetailRowHeight',index);
                },
                onLoadSuccess:function(){
                    setTimeout(function(){
                        $('#dg').datagrid('fixDetailRowHeight',index);
                    },0);
                }
            });
            $('#dg').datagrid('fixDetailRowHeight',index);
        },
        onDblClickRow:function(index,row){
            if(row.number == row.returnNumber){
                $.messager.alert("警告","所有物品已经还完！")
            }else{
                $('#dlg-bt').dialog('open').dialog('center').dialog('setTitle','新增报停信息');
                $('#fm-bt').form('clear');
                $("#detailId").val(row.id);
                url = '/Lease/saveStopped.json';
            }
        }
    });
    $('#prodId').combogrid({
        panelWidth:450,
        idField:'id',
        textField:'name',
        url:'/Lease/product.json?page=0&rows=0',
        columns:[[
            {field:'id',title:'ID'},
            {field:'name',title:'名称'},
            {field:'model',title:'型号'},
            {field:'price',title:'价格'},
            {
                field:'once',
                title:'一次性服务',
                formatter:function(value,row,index){
                    if(value){
                        return "是";
                    }else{
                        return "否";
                    }
                }
            },
            {field:'remark',title:'备注'}
        ]],
       mode:'remote'
    });
});